<?php
namespace Modules\Controller;

use Modules\Traiter\Alisms;
use Modules\Traiter\Alioss;
use Src\Redis;
use Src\Pdo;
use Modules\Model\TestModel;
use Modules\View\TestView;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use function Swoole\Coroutine\go;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\defer;

class Test extends \Src\Controller
{
    use Alioss;//阿里云oss存储trait
    //use Alisms;//阿里云短信发送

    protected $model, $view;
    protected $objectName;//本类名
    
	public function __construct()
    {
        parent::__construct();
        $this->objectName = basename(str_replace('\\', '/', __CLASS__));
        $this->model = new TestModel();
        $this->view = new TestView();
	}
	
	public function __destruct() {}

    //接口例子
    public function test()
    {
        $sql = "select * from user";
        $sqlCount = "select count(*) as count from user";
        $item = 10;
        $page = 0;
        $result = $this->model->pagingPage($item, $page, $sql, $sqlCount);
        return $this->view->__FUNCTION__('200', 'message: ok', $result);
    }

    //协程例子
    public function test2()
    {
		$result = [];//结果集
		$channel = new Channel(8);
		for ($c = 3; $c--;) {
			Coroutine::create(function () use ($channel, $c) {
				$pdo = new pdo();
				for ($n = 100; $n--;) {
					$pdo->select('SELECT * FROM `user`');
				}
				$channel->push(['B'.$c => 1]);
				//关闭协程后，走到这个方法,主要做连接类的清理
//				defer(function () use ($pdo) {
//					$pdo->close();//协程结束后走到此，自行设置对链接对象设置null,不推荐用close来手动结束
//				});
			});
		}
		for ($c = 5; $c--;) {
			Coroutine::create(function () use ($channel, $c) {
				$redis = new Redis();
				$res = $redis->get('aaa');
				$channel->push(['A'.$c => $res]);
//				defer(function () use ($redis) {
//					$redis->close();//协程结束后走到此，自行设置对链接对象设置null,不推荐用close来手动结束
//				});
			});
		}
		for ($i = 0; $i < 5; $i++)//这里如果i<总携程数，那么父协程将不会等待剩余的子协程
		{
			$result += $channel->pop();
		}
		//$this->logger->loging('11.log','22');
		return $this->view->__FUNCTION__('200', 'message: ', $result);
    }

    //smarty例子
    public function test3()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $companyname = isset($_POST['companyname']) ? $_POST['companyname'] : '';
            $username = isset($_POST['username']) ? $_POST['username'] : '';
            $phone = isset($_POST['phone']) ? $_POST['phone'] : '';
            $channel = new Channel(1);
            go(function () use ($channel) {
                $imgurl = $this->image_Upload();//单文件上传
                $channel->push($imgurl);
            });
            for ($i = 0; $i < 1; $i++)//这里如果i<总携程数，那么父协程将不会等待剩余的子协程
            {
                $imgurl= $channel->pop();
            }
            //如果文件上传失败
            if($imgurl == false or $username == '') {
                return $this->view->test3('show','你上传的文件超过2mb或者用户名为空', "/{$this->objectName}/".__FUNCTION__.'/');
            } else {
                $this->model->insertApply($companyname, $username, $phone, $imgurl);
                return $this->view->test3('show','资料已收到，请静待管理员审核,并留意手机短信通知', "/{$this->objectName}/".__FUNCTION__.'/');
            }
        }
        return $this->view->test3();
    }
}
